package com.website.view.menu
{
    import com.greensock.*;
    import com.website.control.PathManager;
    import com.website.util.Constant;
    
    import flash.display.*;
    import flash.events.*;

    public class NavMenu extends Sprite
    {
		public var myXMLList:XMLList;
		public var btnsArray:Array;
		public var navMenu:NavMenu;
        public var menuType:String;
		public var navMenuArray:Array;
		public var menuList:XMLList;
        public var isNavMenu:Boolean;
        public var currNavId:int;
		public var labelList:XMLList;
		public var XMLLength:int;
		public var myTier:String;
		public var navItem:NavItem;
		public var paginatedMenu:PaginatedMenu;
		public var triPaginatedMenu:TriPaginatedMenu;
		public var datePaginatedMenu:DatePaginatedMenu;
		public var alphabetMenu:AlphabetMenu;

        public function NavMenu(tier:String, nameStr:String, xmlList:XMLList) : void
        {
            menuType = "NavMenu";
            isNavMenu = true;
            myXMLList = xmlList;
            XMLLength = myXMLList.length();
            labelList = new XMLList(myXMLList.@label);
            menuList = new XMLList(myXMLList.@menu);
            myTier = tier;
            name = nameStr;
            navMenuArray = new Array();
            identifyCol();
        }

        private function autoClick() : void
        {
            if (btnsArray == null)
            {
            }
            switchCols(currNavId);
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, Constant.fadeSpeed, {alpha:0, overwrite:true, onComplete:function ():void
            {
                visible = false;
            }
            });
            return;
        }

		private function doResize() : void
        {
            var index:int = 0;
            if (navMenuArray.length > 0)
            {
                while (index < navMenuArray.length)
                {
                    
                    if (navMenuArray[index] != undefined)
                    {
                        if (name == "countryMenu" && navMenuArray[index].menuType == "PaginatedMenu")
                        {
                            navMenuArray[index].x = Math.round(2 * (Constant.unitW + Constant.gutterW));
                        }
                        else
                        {
                            navMenuArray[index].x = Math.round(Constant.unitW + Constant.gutterW);
                        }
                    }
					index++;
                }
            }
            if (btnsArray != null)
            {
				index = 0;
                while (index < btnsArray.length)
                {
                    
                    if (btnsArray[index].field != undefined)
                    {
                        var _loc_3:* = Constant.unitW / 75;
                        btnsArray[index].field.scaleY = Constant.unitW / 75;
                        btnsArray[index].field.scaleX = _loc_3;
                    }
                    btnsArray[index].bg.width = Constant.unitW;
                    btnsArray[index].bg.height = btnsArray[index].field.height;
                    btnsArray[index].x = 0;
                    btnsArray[index].y = Math.round(14 * index * Constant.unitW / 75);
					index++;
                }
            }
            return;
        }

		private function buildSubCol(sunColIndex:int) : void
		{
			switch(myTier)
			{
				case "tier0":
				{
					if (myXMLList[sunColIndex].@menu == "paginatedMenu")
					{
						paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
						navMenuArray[sunColIndex] = paginatedMenu;
						addChild(paginatedMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "triPaginatedMenu")
					{
						triPaginatedMenu = new TriPaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].tier1);
						navMenuArray[sunColIndex] = triPaginatedMenu;
						addChild(triPaginatedMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "pressMenu")
					{
						navMenu = new NavMenu( "tier1", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier1);
						navMenuArray[sunColIndex] = navMenu;
						navMenu.visible = false;
						navMenu.alpha = 0;
						addChild(navMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "generalInfoMenu")
					{
						paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
						navMenuArray[sunColIndex] = paginatedMenu;
						addChild(paginatedMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "employMenu")
					{
						navMenu = new NavMenu( "tier1", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier1);
						navMenuArray[sunColIndex] = navMenu;
						navMenu.visible = false;
						navMenu.alpha = 0;
						addChild(navMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "publicationsMenu")
					{
						navMenu = new NavMenu( "tier1", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier1);
						navMenuArray[sunColIndex] = navMenu;
						navMenu.visible = false;
						navMenu.alpha = 0;
						addChild(navMenu);
					}
					else
					{
						navMenu = new NavMenu( "tier1", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier1);
						navMenuArray[sunColIndex] = navMenu;
						navMenu.visible = false;
						navMenu.alpha = 0;
						addChild(navMenu);
					}
					break;
				}
				case "tier1":
				{
					if (name == "publicationsMenu")
					{
						if (myXMLList[sunColIndex].@menu == "recentMenu")
						{
							paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
							navMenuArray.push(paginatedMenu);
							addChild(paginatedMenu);
						}
						else if (myXMLList[sunColIndex].@menu == "triPaginatedMenu")
						{
							triPaginatedMenu = new TriPaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].tier3);
							navMenuArray.push(triPaginatedMenu);
							addChild(triPaginatedMenu);
						}
					}
					else if (myXMLList[sunColIndex].@menu == "alphabetMenu")
					{
						alphabetMenu = new AlphabetMenu( myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier2);
						navMenuArray[sunColIndex] = alphabetMenu;
						addChild(alphabetMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "paginatedMenu")
					{
						paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
						navMenuArray[sunColIndex] = paginatedMenu;
						addChild(paginatedMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "datePaginatedMenu")
					{
						datePaginatedMenu = new DatePaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].tier2);
						navMenuArray[sunColIndex] = datePaginatedMenu;
						addChild(datePaginatedMenu);
					}
					else if (myXMLList[sunColIndex].@menu == "publicationsMenu")
					{
						navMenu = new NavMenu( "tier2", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier2);
						navMenuArray[sunColIndex] = navMenu;
						navMenu.visible = false;
						navMenu.alpha = 0;
						addChild(navMenu);
					}
					else
					{
						navMenu = new NavMenu( "tier2", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier2);
						navMenuArray[sunColIndex] = navMenu;
						navMenu.visible = false;
						navMenu.alpha = 0;
						addChild(navMenu);
					}
					break;
				}
				case "tier2":
				{
					switch(name)
					{
						case "typeMenu":
						{
							paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
							navMenuArray.push(paginatedMenu);
							addChild(paginatedMenu);
							break;
						}
						case "dateMenu":
						{
							if (myXMLList[sunColIndex].@menu == "datePaginatedMenu")
							{
								datePaginatedMenu = new DatePaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].tier3);
								navMenuArray.push(datePaginatedMenu);
								addChild(datePaginatedMenu);
							}
							else if (myXMLList[sunColIndex].@menu == "paginatedMenu")
							{
								paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
								navMenuArray.push(paginatedMenu);
								addChild(paginatedMenu);
							}
							break;
						}
						case "contactMenu":
						{
							break;
						}
						case "continentMenu":
						{
							navMenu = new NavMenu( "tier3", myXMLList[sunColIndex].@menu, myXMLList[sunColIndex].tier3);
							navMenuArray.push(navMenu);
							navMenu.visible = false;
							navMenu.alpha = 0;
							addChild(navMenu);
							break;
						}
						case "paginatedMenu":
						{
							break;
						}
						case "publicationsMenu":
						{
							if (myXMLList[sunColIndex].@menu == "recentMenu")
							{
								paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
								navMenuArray.push(paginatedMenu);
								addChild(paginatedMenu);
							}
							else if (myXMLList[sunColIndex].@menu == "triPaginatedMenu")
							{
								triPaginatedMenu = new TriPaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].tier3);
								navMenuArray.push(triPaginatedMenu);
								addChild(triPaginatedMenu);
							}
							break;
						}
						case "triPaginatedMenu":
						{
							break;
						}
						case "staffSubMenu":
						{
							paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].item);
							navMenuArray.push(paginatedMenu);
							addChild(paginatedMenu);
							break;
						}
						default:
						{
							break;
						}
					}
					break;
				}
				case "tier3":
				{
					switch(name)
					{
						case "countryMenu":
						case "paginatedMenu":
						{
							paginatedMenu = new PaginatedMenu( myXMLList[sunColIndex].@label, myXMLList[sunColIndex].tier4);
							navMenuArray[sunColIndex] = paginatedMenu;
							addChild(paginatedMenu);
							break;
						}
						default:
						{
							break;
						}
					}
					break;
				}
				case "tier4":
				{
					break;
				}
				default:
				{
					break;
				}
			}
//			return;
		}

		private function buildCol() : void
        {
            var _loc_1:int = 0;
            btnsArray = new Array();
            _loc_1 = 0;
            while (_loc_1 < XMLLength)
            {
                var navItem:NavItem =  new NavItem(labelList[_loc_1], menuList[_loc_1], _loc_1);
                btnsArray[_loc_1] = navItem;
				navItem.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
				navItem.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
				navItem.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                addChild(navItem);
                buildSubCol(_loc_1);
                _loc_1 += 1;
                doResize();
            }
            labelList = null;
            menuList = null;
            return;
        }

        public function fadeInShellAuto(sunColIndex:int) : void
        {
            visible = true;
            TweenMax.to(this, Constant.fadeSpeed, {alpha:1, overwrite:true, delay:Constant.fadeSpeed});
            switchColsAuto(sunColIndex);
            return;
        }


        private function onResizer(event:Event):void
        {
            doResize();
            return;
        }

		private function switchCols(sunColIndex:Number) : void
        {
//			return;
            var _loc_2:int = 0;
            if (name != "newsMenu")
            {
                fadeBtns();
                trace("myTier " + myTier + ", btnsArray[currNavId].field.text " + btnsArray[currNavId].field.text);
                PathManager.updateMenuArray(myTier, btnsArray[currNavId].field.text);
                if (navMenuArray.length > 0)
                {
                    _loc_2 = 0;
                    while (_loc_2 < navMenuArray.length)
                    {
                        
                        if (_loc_2 == sunColIndex)
                        {
                            navMenuArray[_loc_2].visible = true;
                            navMenuArray[_loc_2].fadeInShell();
                        }
                        else if (navMenuArray[_loc_2] != undefined)
                        {
                            navMenuArray[_loc_2].fadeOutShell();
                        }
                        _loc_2++;
                    }
                }
            }
            else
            {
                PathManager.updateMenuArray("news", "news");
            }
            return;
        }

		private function toLowercase(str:String) : String
        {
            var reStr:String = null;
            var index:int = 0;
            while (index < str.length)
            {
                
                if (str.charAt(index) == " ")
                {
					reStr = reStr + "-";
                }
                else
                {
					reStr = reStr + str.charAt(index).toLowerCase();
                }
				index = index + 1;
            }
            return reStr;
        }

		private function fadeBtns() : void
        {
            var index:int = 0;
            while (index < btnsArray.length)
            {
                
                if (index != currNavId)
                {
                    TweenMax.to(btnsArray[index].field, Constant.fadeSpeed, {tint:Constant.coldColor, overwrite:true});
                }
                else
                {
                    TweenMax.to(btnsArray[index].field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
                }
				index++;
            }
            return;
        }

		private function btnRollOut(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.coldColor, overwrite:true});
            }
            return;
        }

		private function switchColsAuto(sunColIndex:Number) : void
        {
            if (name == "newsMenu")
            {
            }
            else
            {
                var length:int = btnsArray.length;
                var index:int = 0;
                while (index < length)
                {
                    
//                    if (toLowercase(btnsArray[index].field.text) == myRoot.pathNames[sunColIndex])
//                    {
                        currNavId = btnsArray[index].idNum;
                        TweenMax.to(btnsArray[index].field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
                        navMenuArray[index].visible = true;
                        navMenuArray[index].fadeInShellAuto((sunColIndex + 1));
//                    }
//                    else
//                    {
//                        TweenMax.to(btnsArray[index].field, Constant.fadeSpeed, {tint:Constant.coldColor, overwrite:true});
//                        navMenuArray[index].fadeOutShell();
//                    }
						index++;
                }
            }
            return;
        }

		private function btnRollOver(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.overColor, overwrite:true});
            }
            else
            {
                TweenMax.to(event.currentTarget.field, Constant.fadeSpeed, {tint:Constant.hotColor, overwrite:true});
            }
            return;
        }

		private function btnClick(event:MouseEvent) : void
        {
            currNavId = event.currentTarget.idNum;
            if (navMenuArray[currNavId] == undefined)
            {
                doResize();
                switchCols(event.currentTarget.idNum);
            }
            else
            {
                switchCols(event.currentTarget.idNum);
            }
            return;
        }


		private function identifyCol() : void
        {
            switch(myTier)
            {
                case "tier0":
                {
                    switch(name)
                    {
                        case "projectsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "practiceMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "contactMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "newsMenu":
                        {
                            break;
                        }
                        case "bookmarksMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier1":
                {
                    switch(name)
                    {
                        case "architectureMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "productsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "exhibitionsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "pressMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "employMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "staffMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "publicationsMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier2":
                {
                    switch(name)
                    {
                        case "typeMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "dateMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "continentMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "publicationsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "staffSubMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier3":
                {
                    switch(name)
                    {
                        case "countryMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier4":
                {
                    switch(name)
                    {
                        case "provinceMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "paginatedMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        public function fadeInShell() : void
        {
            if (name == "newsMenu")
            {
            }
            visible = true;
            TweenMax.to(this, Constant.fadeSpeed, {alpha:1, overwrite:true, delay:Constant.fadeSpeed});
            autoClick();
            return;
        }
		
    }
}
